package algorithms.que13;

import edu.princeton.cs.algs4.StdOut;

import java.util.Stack;

/**
 * @功能
 * @创建时间 2020/7/3
 */
public class q139 {
    public static void main(String[] args) {
        Stack<String> a = new Stack<>();
        Stack<String> b = new Stack<>();
        String c = "1+2)*3-4)*5-6)))";
        char[] d = c.toCharArray();
        for(char e :d){
            String f = String.valueOf(e);
            if(f.equals("+")||f.equals("-")||f.equals("*")||f.equals("/"))
                a.push(f);
            else if(f.equals(")")) {
                String g = ")"+b.pop()+a.pop()+b.pop()+"(";
                b.push(g);
            }else {
                b.push(f);
            }
        }
        String result = b.pop();
        // output: result: )))6-5(*)4-3((*)2+1((
        StdOut.println("result: " + result);

        // 更改逆序为顺序
        String seq = "";
        for (int i = result.length()-1; i >= 0; i--){
            seq += result.charAt(i);
        }

        // output: seq: ((1+2)*((3-4)*(5-6)))
        StdOut.println("seq: " + seq);
    }
}
